﻿using AutoMapper;
using MediatR;
using Traceable.App.Application.Command.Aquaculture;
using Traceable.App.DTOS;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Aquaculture
{
    public class SelectCompanyHandler : IRequestHandler<SelectCompanyCommand, ApiResult<EnterpriseDto>>
    {
        private readonly IBaseRepository<Enterprise_Table> _enterpriseRepo;
        private readonly IMapper _mapper;

        public SelectCompanyHandler(IBaseRepository<Enterprise_Table> enterpriseRepo, IMapper mapper)
        {
            _enterpriseRepo = enterpriseRepo;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取公司企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<EnterpriseDto>> Handle(SelectCompanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<EnterpriseDto> result = new ApiResult<EnterpriseDto>();

            var enterprise =_enterpriseRepo.GetAll().Where(p=>p.Batch_Id==request.Batch_Id && p.Traceable_Id==request.Traceable_Id).FirstOrDefault();

            if (enterprise!=null)
            {
                result.Code = StatusEnum.OK;
                result.Data=_mapper.Map<EnterpriseDto>(enterprise);
                result.Message = "获取成功";
            }
            return Task.FromResult(result);
        }
    }
}
